const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    winWidth: 0,
    winHeight: 0,
    currentMonth:"",
    // tab切换
    currentTab: 1,
    performance: 2400,
    yeji: 0,
    dateStr: "2020-01",
    dabiao: "未达标",
    beInvitedLevel: "爱⁺传播大使"
  },
  myRequest:function(date){
    var that = this;

    wx.getStorage({
      key: 'openid',
      success: function(res) {
        let openid = res.data
        wx.request({
          url: app.globalData.URL + '/app/myYeji/index?dateStr=' + date + '&openid=' + openid,
          success: function (res) {
            console.log("请求得到的日期", res.data.dateStr)
            that.setData({
              performance: res.data.performance,
              yeji: res.data.yeji,
              dateStr: res.data.dateStr,
              dabiao: res.data.dabiao,
              beInvitedLevel: res.data.beInvitedLevel
            })
          }
        })
      },
    })
    
    
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    // 获取系统信息
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight
        });
      }
    });
    var timestamp = Date.parse(new Date());
    var date = new Date(timestamp);
    //获取年份  
    var Y = date.getFullYear();
    //获取月份  
    var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
    
    var currentDate = Y + '-' + M
    console.log("第一次当前日期",currentDate)
    that.myRequest(currentDate)
  },

  // 滑动切换tab
  bindChange: function (e) {
    var that = this;
    that.setData({ currentTab: e.detail.current });
  },
  //单位数的月份前面加 0
  checkMonth:function(i) {
    if (i < 10) {
      i = "0" + i;
    }
    return i;
  },
  // 点击tab切换
  swichNav: function (e) {
    var that = this;
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current
      })
    }

    
    let currentDate = new Date(that.data.dateStr); 
    //计算上个月
    if(that.data.currentTab == 0){
      
      // 月份加一
      let lastDate = currentDate.setMonth(currentDate.getMonth() - 1); // 输出日期格式为毫秒形式1551398400000

      lastDate = new Date(lastDate);
      let lastYear = lastDate.getFullYear();
      let lastMonth = that.checkMonth(lastDate.getMonth() + 1); // 因日期中的月份表示为0-11，所以要显示正确的月份，需要 + 1

      lastDate = lastYear + '-' + lastMonth;

      that.myRequest(lastDate)

    } else if (that.data.currentTab == 1){
      var timestamp = Date.parse(new Date());
      var date = new Date(timestamp);
      //获取年份  
      var Y = date.getFullYear();
      //获取月份  
      var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);

      var currentDate1 = Y + '-' + M
      console.log("点击当前年月tab",currentDate1)
      that.myRequest(currentDate1)

    }else{
      // 月份减一
      let nextDate = currentDate.setMonth(currentDate.getMonth() + 1); // 输出日期格式为毫秒形式1556668800000

      nextDate = new Date(nextDate);
      let nextYear = nextDate.getFullYear();
      let nextMonth = that.checkMonth(nextDate.getMonth() + 1); // 因日期中的月份表示为0-11，所以要显示正确的月份，需要 + 1

      nextDate = nextYear + '-' + nextMonth;
      
      that.myRequest(nextDate)
    }

  }
})